package com.ruoyi.receipt.controller;

import java.util.List;

import com.ruoyi.receipt.domain.TbExampleSubjectManager;
import com.ruoyi.receipt.service.ITbExampleSubjectManagerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.receipt.domain.TbSubjectCluste;
import com.ruoyi.receipt.service.ITbSubjectClusteService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 题簇审核Controller
 * 
 * @author ruoyi
 * @date 2021-03-29
 */
@RestController
@RequestMapping("/receipt/subjectClusteAudit")
public class TbSubjectClusteAuditController extends BaseController
{
    @Autowired
    private ITbSubjectClusteService tbSubjectClusteService;

    @Autowired
    private IdWorker idWorker;
    @Autowired
    ITbExampleSubjectManagerService tbExampleSubjectManagerService;

    /**
     * 查询题簇审核列表
     */
    @PreAuthorize("@ss.hasPermi('receipt:subjectClusteAudit:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbSubjectCluste tbSubjectCluste)
    {
        startPage();
        List<TbSubjectCluste> list = tbSubjectClusteService.selectTbSubjectClusteList(tbSubjectCluste);
        return getDataTable(list);
    }

    /**
     * 导出题簇审核列表
     */
    @PreAuthorize("@ss.hasPermi('receipt:subjectClusteAudit:export')")
    @Log(title = "题簇审核", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbSubjectCluste tbSubjectCluste)
    {
        List<TbSubjectCluste> list = tbSubjectClusteService.selectTbSubjectClusteList(tbSubjectCluste);
        ExcelUtil<TbSubjectCluste> util = new ExcelUtil<TbSubjectCluste>(TbSubjectCluste.class);
        return util.exportExcel(list, "clusteAudit");
    }

    /**
     * 获取题簇审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('receipt:subjectClusteAudit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbSubjectClusteService.selectTbSubjectClusteById(id));
    }

    /**
     * 新增题簇审核
     */
    @PreAuthorize("@ss.hasPermi('receipt:subjectClusteAudit:add')")
    @Log(title = "题簇审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbSubjectCluste tbSubjectCluste)
    {
        String clusteName = tbSubjectCluste.getClusteName();
        //根据名字去查询是否存在
        List<TbSubjectCluste> tbSubjectClustes = tbSubjectClusteService.selectName(clusteName);
        if (tbSubjectClustes.size()>0){
            return AjaxResult.error("已经存在该题簇，添加失败");
        }else {
            tbSubjectCluste.setId(idWorker.nextId());
            return toAjax(tbSubjectClusteService.insertTbSubjectCluste(tbSubjectCluste));
        }

    }

    /**
     * 修改题簇审核
     */
    @PreAuthorize("@ss.hasPermi('receipt:subjectClusteAudit:edit')")
    @Log(title = "题簇审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbSubjectCluste tbSubjectCluste)
    {
        String clusteName = tbSubjectCluste.getClusteName();
        //根据名字去查询是否存在
        List<TbSubjectCluste> tbSubjectClustes = tbSubjectClusteService.selectName(clusteName);
        if (tbSubjectClustes.size()>0){
            return AjaxResult.error("已经存在该题簇，添加失败");
        }else {
            tbSubjectCluste.setStatus(1);
            return toAjax(tbSubjectClusteService.updateTbSubjectCluste(tbSubjectCluste));
        }
    }
    /**
     * 审核通过
     */

    @PreAuthorize("@ss.hasPermi('receipt:subjectClusteAudit:Consent')")
    @PutMapping("/ClusteauditConsent")
    public AjaxResult ClusteauditConsent(@RequestBody TbSubjectCluste tbSubjectCluste){
        return toAjax(tbSubjectClusteService.updateTbSubjectCluste(tbSubjectCluste));
    }

    /**
     * 审核驳回
     */
    @PreAuthorize("@ss.hasPermi('receipt:subjectClusteAudit:Reject')")
    @PutMapping("/ClusteauditReject")
    public AjaxResult ClusteAuditReject(@RequestBody TbSubjectCluste tbSubjectCluste){

        return toAjax(tbSubjectClusteService.updateTbSubjectCluste(tbSubjectCluste));
    }

    /**
     * 删除题簇审核
     */
    @PreAuthorize("@ss.hasPermi('receipt:subjectClusteAudit:remove')")
    @Log(title = "题簇审核", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids)
    {
        TbSubjectCluste tbSubjectCluste = tbSubjectClusteService.selectTbSubjectClusteById(ids);
        //去查询是否有例题绑定题簇 如果有就不能删除
        TbExampleSubjectManager tbExampleSubjectManager=new TbExampleSubjectManager();
        tbExampleSubjectManager.setSubjectClusteId(ids);
        List<TbExampleSubjectManager> tbExampleSubjectManagers = tbExampleSubjectManagerService.selectTbExampleSubjectManagerList(tbExampleSubjectManager);
        if (tbExampleSubjectManagers.size()>0){
            return  AjaxResult.error("该题簇下面有例题绑定，不能删除");
        }else {
            tbSubjectCluste.setIsDel(1);
            int i = tbSubjectClusteService.updateTbSubjectCluste(tbSubjectCluste);
            return AjaxResult.success();
        }

    }
}
